package c.francis.controller;
import java.util.Date;
import java.util.List;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import c.francis.utils.CookieUtils;
import c.francis.utils.HostHolder;
import c.francis.utils.HotelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import c.francis.pojo.Member;
import c.francis.service.impl.MemberServiceImpl;
@Controller
public class MemberController {
	@Autowired
	private MemberServiceImpl memberServiceImpl;

	@Autowired
	private HostHolder	hostHolder;

	@Value("${server.servlet.context-path}")
	private String	contextPath;

	@RequestMapping("checkLogin")
	public @ResponseBody Member checklogin(HttpServletRequest request, String email, HttpServletResponse response, Model	model) {


		Member member = memberServiceImpl.queryMember(email);

		String ticket=HotelUtils.generateUUID()+System.currentTimeMillis();
		memberServiceImpl.updateTicketByEmail(email,ticket);

		Cookie	cookie=new Cookie("ticket",ticket);
		cookie.setPath(contextPath);
		cookie.setMaxAge(1000*60*60);

		response.addCookie(cookie);
		//hostHolder.setUser(member);
		//Member user = hostHolder.getUser();
		//System.out.println("================="+user);



//		System.out.println("================");
//		System.out.println(member);
//		HttpSession session = request.getSession();
//		session.setAttribute("member", member);
//		System.out.println("----------------------");
		model.addAttribute("member",member);
		return member;
	}

//	@RequestMapping("checkLogin")
//	public @ResponseBody String checklogin(HttpServletRequest request, String email) {
//		Member member = memberServiceImpl.queryMember(email);
//
//		HttpSession session = request.getSession();
//		session.setAttribute("member", member);
//
//		Map<String,Object> map=new HashMap<>();
//		map.put("member",member);
//
//		return hotelUtils.getJSONString(1,"查询成功",map);
//	}
	
	@RequestMapping("gomember")
	public String gomembers(HttpServletRequest request) {


//		HttpSession session = request.getSession();
//		Member member  = (Member)session.getAttribute("member");

		String ticket = CookieUtils.getValue(request, "ticket");
		Member member = memberServiceImpl.findMemberByTicket(ticket);

		if(member==null) {
			return "login";
		}else {
			return "membercenter";
		}
	}
	
	@RequestMapping("creNewMember")
	public @ResponseBody int createNewMember(String email,String password) {
		Date regtime = new Date();
		int flag = 0;
		String[] split = email.split("@");
		String	username	=	split[0];
		//System.out.println("----------username="+username);
		try {
			Member member = memberServiceImpl.queryMember(email);
			if(member!=null) {
				flag = 2;
			}else {
				//System.out.println("--------");
				memberServiceImpl.insertNewMembers(username,email, password, regtime);
				flag = 1;
			}
		} catch (Exception e) {
			flag = 3;
		}
		return flag;
	}
	
	@RequestMapping("goupdatelink")
	public @ResponseBody Member goupdate(HttpServletRequest request) {
		HttpSession session = request.getSession();
		Member member = (Member)session.getAttribute("member");
		return member;
	}
	@RequestMapping("goupdate")
	public String goupdate() {
		return "updatelink";
	}
	
	
	
	@RequestMapping("updatelink")
	public @ResponseBody int updatelink(String name,String phone,HttpServletRequest request) {
		int flag = 0 ;
//		HttpSession session = request.getSession();
//		Member member = (Member)session.getAttribute("member");

		String ticket = CookieUtils.getValue(request, "ticket");
		Member member = memberServiceImpl.findMemberByTicket(ticket);

		int mid = member.getId();
		try {
			memberServiceImpl.updateLinkInfo(mid, name, phone);
			flag = 1;

			//Member member2 = memberServiceImpl.queryMember(member.getEmail());
			//session.setAttribute("member", member2);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	
	@RequestMapping("goupdatepwd")
	public String goupdatepwd() {
		return "updatepwd";
	}
	
	
	@RequestMapping("updatepwd")
	public @ResponseBody int updatepwd(String newpassword,String oldpassword,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Member member = (Member)session.getAttribute("member");
		if(!oldpassword.equals(member.getPassword())) {
			return 2;//ԭ�������
		}else {
			try {
				memberServiceImpl.udpatePwd(member.getId(), newpassword);
				return 1;
			} catch (Exception e) {
				e.printStackTrace();
				return 0;
			}
		}
	}
	
	
	@RequestMapping("queryallmember")
	public ModelAndView queryAllMember(@RequestParam("pagenum")int pagenum) {
		ModelAndView modelAndView = new ModelAndView();
		int pagestart = 0;
		if(pagenum<=0) {
			pagestart = 0;
		}else {
			pagestart = (pagenum-1)*8;
		}
		List<Member> members = memberServiceImpl.queryAllMember(pagestart);
		modelAndView.addObject("members", members);
		int count = memberServiceImpl.countpage();
		if(pagenum>=count) {
			pagenum = count;
		}else if(pagenum<=0) {
			pagenum=1;
		}
		modelAndView.addObject("pagenum", pagenum);
		modelAndView.setViewName("memberstatistic");
		return modelAndView;
	}

}
 